require "prefabutil"

local assets =
{
    Asset("ANIM", "anim/bboy_dry_well.zip"),
    Asset("ANIM", "anim/ui_board_5x3.zip"),
}

local prefabs =
{
    "collapse_small",
}

local function onhammered(inst, worker)
    inst.components.lootdropper:DropLoot()
    local fx = SpawnPrefab("collapse_small")
    fx.Transform:SetPosition(inst.Transform:GetWorldPosition())
    fx:SetMaterial("wood")
    inst:Remove()
end

local function onhit(inst, worker)
    -- inst.AnimState:PlayAnimation("hit")
    -- inst.AnimState:PushAnimation("idle", false)
end

local function onsave(inst, data)
end

local function onload(inst, data)
end

local function onbuilt(inst)
    -- inst.SoundEmitter:PlaySound("dontstarve/common/sign_craft")
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddMiniMapEntity()
    inst.entity:AddNetwork()

    inst:SetDeploySmartRadius(0.75) -- recipe min_spacing/2
    MakeObstaclePhysics(inst, .5)

    inst.MiniMapEntity:SetIcon("bboy_dry_well.png")

    inst.AnimState:SetBank("bboy_dry_well")
    inst.AnimState:SetBuild("bboy_dry_well")
    inst.AnimState:PlayAnimation("idle")

    inst.Transform:SetScale(1.2, 1.2, 1)

    MakeSnowCoveredPristine(inst)

    inst:AddComponent("talker")

    inst:AddTag("_bboy_travelable")
    inst:AddTag("structure")
    inst:AddTag("sign")

    -- 初始化
    inst:AddTag("_writeable")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    -- 删除标签，以便在复制以下组件时可以正确添加它们
    inst:RemoveTag("_writeable")
    inst:RemoveTag("_bboy_travelable")

    inst:AddComponent("bboy_travelable")
    inst:AddComponent("inspectable")
    inst:AddComponent("writeable")
    inst:AddComponent("lootdropper")

    inst:AddComponent("workable")
    inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
    inst.components.workable:SetWorkLeft(4)
    inst.components.workable:SetOnFinishCallback(onhammered)
    inst.components.workable:SetOnWorkCallback(onhit)
    MakeSnowCovered(inst)

    inst.OnSave = onsave
    inst.OnLoad = onload

    MakeHauntableWork(inst)
    inst:ListenForEvent("onbuilt", onbuilt)

    return inst
end

return Prefab("bboy_dry_well", fn, assets, prefabs),
    MakePlacer("bboy_dry_well_placer", "bboy_dry_well", "bboy_dry_well", "idle", nil, nil, nil, 1.2)
